from selenium.webdriver.common.by import By
from frp_page_object.base_page import BasePage, logger
import time


class NewSalesOrderPage(BasePage):
    loc_new_product_button = (By.XPATH, '//*[contains(text(),"新增商品")]')
    loc_sales_policy = (By.ID, 'salePolicyCode')
    loc_sales_policy_items = (By.XPATH, '//*[@id="salePolicyCode_list"]/div')
    loc_product_name = (By.ID, 'itemCode')
    loc_product_name_item = (By.ID, 'itemCode_list_0')
    loc_price = (By.ID, 'price')
    loc_owner = (By.ID, 'orgCode')
    loc_owner_items = (By.XPATH, '//*[@id="orgCode_list"]/div')
    loc_quantity = (By.ID, 'qty')
    loc_date = (By.ID, 'lastDeliverDate')
    loc_transport_mode = (By.XPATH, '//*[@id="transportMode"]/../../span[2]')
    loc_transport_mode_items = (By.XPATH, '//*[@id="transportMode_list"]/div')
    loc_new_product_ok_button = (By.XPATH, '//*[@class="ant-modal-footer"]//button[2]')

    def click_new_product_button(self):
        self.click(self.loc_new_product_button)

    # 选择销售政策
    def select_sales_policy(self,  sales_policy):
        self.click_text_in(sales_policy,self.loc_sales_policy, self.loc_sales_policy_items)

    # 选择商品名称
    def select_product_name(self, product_name):
        self.input(self.loc_product_name, product_name)
        try:
            self.click(self.loc_product_name_item, 10)
        except Exception:
            logger.error(f'找不到商品：{product_name}')

    def input_price(self, price):
        self.input(self.loc_price, price)

    def select_owner(self, owner):
        self.click_text_in(owner, self.loc_owner, self.loc_owner_items)

    def input_quantity(self, quantity):
        self.input(self.loc_quantity, quantity)

    def input_date(self, date):
        self.input(self.loc_date, date)

    def select_transport_mode(self, transport_mode):
        self.click_text_in(transport_mode, self.loc_transport_mode, self.loc_transport_mode_items)

    def click_ok_button(self):
        self.click_wait(self.loc_new_product_ok_button)

    # 填写新增商品页的所有选项
    def input_all(self, sales_policy, product_name, price, owner, quantity, date, transport_mode):
        self.select_sales_policy(sales_policy)
        self.select_product_name(product_name)
        self.input_price(price)
        self.select_owner(owner)
        self.input_quantity(quantity)
        self.input_date(date)
        self.select_transport_mode(transport_mode)
        self.click_ok_button()
